package com.szmd.gradle.tasks;

import com.android.build.gradle.AppExtension;
import com.android.build.gradle.api.BaseVariant;
import com.android.build.gradle.api.BaseVariantOutput;
import com.google.gson.Gson;
import com.szmd.gradle.Extension;
import com.szmd.gradle.ReleaseHelperPlugin;
import com.szmd.gradle.api.DingTalkInterface;
import com.szmd.gradle.api.PgyerUploadInterface;
import com.szmd.gradle.api.RetrofitUtil;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;

import java.io.File;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * created by : chentl
 * Date: 2021/01/07
 */
public class SendMsgToDingTalkTask extends DefaultTask {

    public BaseVariant baseVariant;
    public Project project;

    public void init(BaseVariant baseVariant, Project project) {
        this.baseVariant = baseVariant;
        this.project = project;
        setDescription("Upload release apk to DingTalk");
        setGroup("release helper");
    }

    public static class ActionCard {
        public String title;
        public String text;
        public int hindAvatar = 0;
        public int btOrientation = 0;
        public String singleTitle;
        public String singleURL;
    }

    public static class DingMsg {
        public ActionCard actionCard = new ActionCard();
        public String msgType = "actionCard";
    }

    @TaskAction
    public void SendMessageToDingTalk() {
        Extension extension = Extension.getConfig(project);
        DingMsg dingMsg = new DingMsg();
        dingMsg.actionCard.title = extension.msgTitle;
        dingMsg.actionCard.text = extension.msgTitle;
        dingMsg.actionCard.singleTitle = extension.msgTitle;
        dingMsg.actionCard.singleURL = extension.msgTitle;

        try {
            Response<ResponseBody> execute = RetrofitUtil
                    .getRetrofit("https://oapi.dingtalk.com/")
                    .create(DingTalkInterface.class)
                    .sendMsgToDingTalk(extension.apiToken,dingMsg)
                    .execute();
            System.out.println("\n2. DingTalkMsgResponse : " + new Gson().toJson(execute.body().toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
